<?php
/*
 * CRMJqueryUiComponent File
 *
 * @link http://riedel-st.de
 * @author Stefan Riedel <sr@riedel-st.de>
*/

Yii::import ( 'littlecrm.web.IJqueryUi' );

/**
 * Description of CRMJqueryUiComponent
 *
 * @author Stefan Riedel
 */
class CRMJqueryUiBase extends CInputWidget implements IJqueryUi {
	
	protected $_options = array ();
	
	protected $_validOptions = array ();
	
	protected $_basePath;
	
	protected $_clientScript;
	
	protected $_cssTheme = 'cupertino';
	
	public function setOptions($options) {
		if (! is_array ( $options )) {
			throw new CException ( 'Options must be an array.' );
		}
		self::checkOptions ( $options, $this->_validOptions );
		$this->_options = $options;
	}
	
	/**
	 * Getter
	 *
	 * @return array
	 */
	public function getOptions() {
		return $this->options;
	}
	
	public function getClientScript() {
		return $this->_clientScript;
	}
	
	public function getBasePath() {
		return $this->_basePath;
	}
	
	public function publishAssets() {
		$jqueryDir = dirname ( __FILE__ ) . DIRECTORY_SEPARATOR . 'jquery';
		$this->_basePath = Yii::app ()->getAssetManager ()->publish ( $jqueryDir );
	}
	
	public function registerClientsScripts() {
		if (empty ( $this->_basePath )) {
			throw new CException ( 'Sorry but the Base Url is empty.' );
		}
		$this->_clientScript = Yii::app ()->getClientScript ();
		$this->_clientScript->registerCoreScript ( 'jquery' );
		$jqueryUiPath = $this->_basePath . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . 'jquery-ui-1.7.2.custom.min.js';
		$jqueryUiCssPath = $this->_basePath . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . $this->_cssTheme . DIRECTORY_SEPARATOR . 'jquery-ui-1.7.2.custom.css';
		$this->_clientScript->registerScriptFile ( $jqueryUiPath );
		$this->_clientScript->registerCssFile ( $jqueryUiCssPath );
	}
	
	protected function _makeOptions(array $options = array()) {
		$encodedOptions = CJavaScript::encode ( $options );
		return $encodedOptions;
	}
	
	protected static function checkOptions($value, $validOptions) {
		if (! empty ( $validOptions )) {
			foreach ( $value as $key => $val ) {
				if (! array_key_exists ( $key, $validOptions )) {
					throw new CException ( Yii::t ( 'CRMJqueryUiBase', '{k} ist kein korrekter Wert.', array ('{k}' => $key ) ) );
				}
				$type = gettype ( $val );
				if ((! is_array ( $validOptions [$key] ['type'] ) && ($type != $validOptions [$key] ['type'])) || (is_array ( $validOptions [$key] ['type'] ) && ! in_array ( $type, $validOptions [$key] ['type'] ))) {
					throw new CException ( Yii::t ( 'CRMJqueryUiBase', '{k} muss vom Typ {t} sein', array ('{k}' => $key, '{t}' => implode ( ',', (is_array ( $validOptions [$key] ['type'] )) ? implode ( ', ', $validOptions [$key] ['type'] ) : $validOptions [$key] ['type'] ) ) ) );
				}
				if (array_key_exists ( 'possibleValues', $validOptions [$key] )) {
					if (! in_array ( $val, $validOptions [$key] ['possibleValues'] )) {
						throw new CException ( Yii::t ( 'CRMJqueryUiBase', '{k} muss eins von: {v} sein.', array ('{k}' => $key, '{v}' => implode ( ', ', $validOptions [$key] ['possibleValues'] ) ) ) );
					}
				}
				if (($type == 'array') && array_key_exists ( 'elements', $validOptions [$key] )) {
					self::checkOptions ( $val, $validOptions [$key] ['elements'] );
				}
			}
		}
	}

}
?>
